package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.ChargeBillMapper;
import com.java.business.dao.CheckinMapper;
import com.java.business.model.Appointment;
import com.java.business.model.ChargeBill;
import com.java.business.model.Checkin;
import com.java.business.model.MaintainRecord;
import com.java.business.service.ChargeBillService;
import com.java.business.service.CheckinService;
import com.java.business.service.MaintainRecordService;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.manage.model.*;
import com.java.manage.service.api.*;
import com.java.userservice.model.User;
import com.java.userservice.service.api.UserServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ChargeBillServiceImpl implements ChargeBillService {

    @Autowired
    private ChargeBillMapper chargeBillMapper;

    @DubboReference
    private CompanyApiService companyApiService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MaintainRecordService maintainRecordService;

    @Override
    public Result list(ChargeBill chargeBill) {
        PageHelper.startPage(chargeBill.getPageNum(), chargeBill.getPageSize());

        String token = request.getHeader("Token");
        Integer storeId = (Integer) JwtUtil.getClaims(token).get("storeId");
        chargeBill.setStoreId(storeId);

        List<ChargeBill> list = chargeBillMapper.list(chargeBill);

        for (ChargeBill cb : list) {
            if (cb.getStoreId() != null) {
                // 用户表
                List<Integer> collect = list.stream().map(ChargeBill::getStoreId).collect(Collectors.toList());
                List<Company> companys = companyApiService.listByIds(collect);
                Map<Integer, Company> userMap = companys.stream().collect(Collectors.toMap(company -> company.getId(), company -> company));
                list.stream().forEach(app -> {
                    app.setCompany(userMap.get(app.getId()));
                });
            }
        }

        PageInfo<ChargeBill> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result save(ChargeBill chargeBill) {
        chargeBillMapper.save(chargeBill);
        return Result.success();
    }

    @Override
    public Result modify(ChargeBill chargeBill) {
        chargeBillMapper.modify(chargeBill);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            chargeBillMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result get(int id) {
        chargeBillMapper.get(id);
        return Result.success();
    }

    @Override
    public Result ToSave(ChargeBill chargeBill) {
        MaintainRecord record = new MaintainRecord();
        chargeBill.setRecordId(String.valueOf(chargeBill.getId()));
        record.setId(chargeBill.getId());
        record.setSpeedStatus(String.valueOf(6));
        maintainRecordService.modify(record);
        chargeBill.setId(null);
        chargeBillMapper.save(chargeBill);
        return Result.success();
    }
}
